package com.geese.module.admin.controller;

import com.geese.common.BaseController;
import com.geese.common.vo.Page;
import com.geese.exception.BusinessException;
import com.geese.module.admin.service.ICashService;
import com.geese.module.admin.vo.CashVO;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 小二财务管理
 * Created by huweiwei on 2017/3/20.
 */
@RestController
@RequestMapping("/admin/cash")
public class CashController extends BaseController{

    @Autowired
    private UserService userService;
    @Autowired
    private ICashService cashService;

    /**
     * 查询提现申请列表
     * @param token
     * @param cashVO
     * @return
     */
    @GetMapping("list")
//    @RequiresPermissions("withdraw:*")
    public String list(@RequestParam String token, CashVO cashVO) {
//        userService.checkUser(token);
        Page page = getPageInfo(cashVO);
        Map<String, Object> result = cashService.getList(page, cashVO);
        return response(result);
    }

    /**
     * 审核单个
     * @param token
     * @param id
     * @return
     */
    @GetMapping("audit")
    public String audit(@RequestParam String token, @RequestParam int id, HttpServletRequest request){
        SecurityUser user = userService.getLoginUser(token);
        Integer oid = cashService.doAudit(id);
        doLog(request, user, "审核申请单，单号为：" + oid);
        return response();
    }

    /**
     * 审核一群
     * @param token
     * @param ids
     * @return
     */
    @GetMapping("auditAll")
    public String auditAll(@RequestParam String token, @RequestParam List<Integer> ids, HttpServletRequest request){
        SecurityUser user = userService.getLoginUser(token);
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(120009);
        }
        List<Integer> oidList = cashService.batchAudit(ids);
        doLog(request, user, "批量审核申请单，单号为：" + oidList);
        return response();
    }

    /**
     * 确认提现
     * @param token
     * @param id
     * @return
     */
    @GetMapping("complete")
    public String complete(@RequestParam String token, @RequestParam int id, HttpServletRequest request) {
        SecurityUser user = userService.getLoginUser(token);
        Integer oid = cashService.doComplete(id);
        doLog(request, user, "确认申请单，单号为：" + oid);
        return response();
    }

    /**
     * 确认提现
     * @param token
     * @param ids
     * @return
     */
    @GetMapping("completeAll")
    public String completeAll(@RequestParam String token, @RequestParam List<Integer> ids, HttpServletRequest request) {
        SecurityUser user = userService.getLoginUser(token);
        List<Integer> oidList = cashService.batchComplete(ids);
        doLog(request, user, "批量确认申请单，单号为：" + oidList);
        return response();
    }
}
